package com.feimt.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feimt.common.R;
import com.feimt.dto.SetmealDto;
import com.feimt.pojo.Category;
import com.feimt.pojo.Dish;
import com.feimt.pojo.Setmeal;
import com.feimt.pojo.SetmealDish;
import com.feimt.service.CategoryService;
import com.feimt.service.DishService;
import com.feimt.service.SetmealDishService;
import com.feimt.service.SetmealService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto:{}",setmealDto);

        // 新增套餐并且关联菜品
        setmealService.saveSetmealAndDish(setmealDto);

        return R.success("新增成功");
    }

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        log.info("page：{}，pageSize:{},name:{}",page,pageSize,name);
        /*1 查询套餐基本信息*/
        //1.1.构建分页条件
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //1.2.构建查询条件  排序
        LambdaQueryWrapper<Setmeal> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(!StringUtils.isEmpty(name),Setmeal::getName,name);
        qw1.orderByDesc(Setmeal::getUpdateTime);
        //1.3.分页查询
        setmealService.page(setmealPage);
        /*2.查询分类名称*/
//        setmeal ---- setmealDto
//        records<Setmeal>    records<SetmealDto>
//        Page<Setmeal>        Page<SetmealDto>
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        /*2.1 分页数据copy*/
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        /*2.2 数据参数*/
        List<SetmealDto> collect = setmealPage.getRecords().stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            // 基本字段
            BeanUtils.copyProperties(item,setmealDto);

            Category category = categoryService.getById(item.getCategoryId());
            if(category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(collect);

        //4.返回数据
        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){
        log.info("ids：{}",ids);
        // 批量删除
        setmealService.deleteByIdsAndDishs(ids);
        return R.success("");
    }

    @Autowired
    private DishService dishService;
    /**
     * 根据套餐分类查询套餐数据
     * 1. 构建条件
     * 2. 查询
     * list?categoryId=1538535903560896514&status  get
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R list(Setmeal setmeal){
        log.info("setmeal:{}",setmeal);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        //        查询套餐
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }

    @Autowired
    private SetmealDishService setmealDishService;

    @GetMapping("/dish/{id}")
    public R dishlist(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> dishs = setmealDishService.list(qw);
        // 根据dish 去查询image ---> SetmealDish
        dishs = dishs.stream().map(item -> {
            Dish d = dishService.getById(item.getDishId());
            item.setImage(d.getImage());
            return item;
        }).collect(Collectors.toList());

        return R.success(dishs);
    }
}
